#Kimi ga Yobu Megido No Oka De LFX parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class KimiGaYobu_LFX(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(KimiGaYobu_LFX,self).__init__("LFX")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self, n=0):
        
        if n:
            name = self.inFile.read_string(n)
            
        else:
            name = ""
            char = self.inFile.read_char()
            while char != "\x00":
                name = ''.join([name, char])
                char = self.inFile.read_char()
            
        return name.split("\x00")[0]
    
    def parse_unk(self, meshName):
        
        unkCount = self.inFile.read_short()
        for i in range(unkCount):
            self.inFile.read_long()
    
    def parse_uv(self, meshName):
        
        numUV = self.inFile.read_short()
        for i in range(numUV):
            tu, tv = self.inFile.read_float(2)
            
    def parse_normals(self, meshName):
        
        numNorm = self.inFile.read_short()
        for i in range(numNorm):
            nx, ny, nz = self.inFile.read_float(3)
            
    def parse_coords(self, meshName):
        
        numCoords = self.inFile.read_short()
        print numCoords
        for i in range(numCoords):
            vx, vy, vz = self.inFile.read_float(3)
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.inFile.read_string(3)
        meshType = self.inFile.read_byte()
        unk1, numMesh, unk3 = self.inFile.read_short(3)
        
        if meshType == 1:
        
            print "%d meshes" %numMesh
            for i in range(numMesh):
                
                start = self.inFile.tell()
                self.inFile.read_float(8)
                self.inFile.read_long(10)
                self.inFile.read_float(1)
                self.inFile.read_byte()
                meshName = self.read_name(40)
                if unk1 == 0x06:
                    texName = self.read_name(16)
                else:
                    texName = self.read_name()
                print meshName, texName
                
                cur = self.inFile.tell()
                print start, cur, cur-start, 193-(cur-start)
                self.inFile.seek(max(193 - (cur - start), 0), 1)
            self.create_object(meshName)    
            
            self.inFile.seek(unk3 + 53)
            
            #vertex section
            self.parse_coords(meshName)
            self.parse_normals(meshName)
            self.parse_uv(meshName)
            self.parse_unk(meshName)
            
            self.inFile.read_byte() #unk
            self.inFile.seek(44, 1) #unk
            self.inFile.read_float(16) #matrix?
            self.inFile.seek(16, 1) #null
            unk4 = self.inFile.read_short()
            numIndices = self.inFile.read_short() #section size: numIndices * 2
            unk6 = self.inFile.read_short()
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = KimiGaYobu_LFX(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "LFX", "LFX", "Kimi ga Yobu, Meggido no Oka de LFX"

if __name__ == '__main__':
    
    file1 = "F03_STUMP_02.LFX"
    file2= "F04_GATE.LFX"
    file3 = "M015.LFX"
    file4 = "F03_STONE_01.LFX"
    obj = read_file(file1)
    obj.join_faces()